package com.mao.crowd.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mao.crowd.mapper.RoleMapper;
import com.mao.crowd.service.api.RoleService;
import entity.Role;
import entity.RoleExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author maomeng
 * @create 2021-06-01- 15:13
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public PageInfo<Role> getPageInfo(Integer pageNum, Integer pageSize, String keyword) {
//        开启分页
        PageHelper.startPage(pageNum,pageSize);
//        查询role列表
        List<Role> roles = roleMapper.selectRoleByKeyWord(keyword);
//        封装为pageInfo对象返回
        return new PageInfo<>(roles);

    }

    @Override
    public void saveRole(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public void removeRole(List<Integer> roleIdList) {

        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIdIn(roleIdList);
        roleMapper.deleteByExample(roleExample);
    }

    @Override
    public List<Role> getAssignedRole(Integer adminId) {
        List<Role> assignedRoleList = roleMapper.selectAssignedRole(adminId);
        return assignedRoleList;
    }

    @Override
    public List<Role> getUnassignedRole(Integer adminId) {
        List<Role> unassignedRoleList = roleMapper.selectUnassignedRole(adminId);
        return unassignedRoleList;
    }
}
